var table;
var layer;
var upload;
var form;
var subImg;
var element;
layui.use(['layer','table','upload','form','element'],function(){
    table=layui.table;
    layer=layui.layer;
    upload = layui.upload;
    form=layui.form;
    element=layui.element;

    //获取当前登陆人员的信息
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    $("#createId").val(create.user_id);
    $("#userId").val(create.user_id);


    //上传图片
    upload.render({
        elem : '#uploadImgBtn',
        url : '/SyteacherSelController/uploadAttachments',//上传接口
        done : function(res) {
            if(res.code==0){

                var url=$("#url").val();
                if(url==""||url==null||url==undefined) {
                    subImg=res.src;
                    //主图截取
                    var arrayImgs=res.src
                    var url=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);
                    $("#url").val(url);
                }

                //获取预览图个数
                var uploadImgNum=document.getElementsByClassName("uploadImg").length;

                //判断预览图是否大于8张，大于8张隐藏
                if(uploadImgNum>7){

                    //预览图计数
                    var num=$("#num").val();
                    if(num==0){
                        //隐藏之后，判断预览图是否大于8张，大于8张添加......
                        var num=document.getElementById('h1_id');
                        if(num!=null){
                            $("#h1_id").replaceWith("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
                            $("#img_div4").append("<img src='"+res.src+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }else{
                            $("#img_div4").append("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
                            $("#img_div4").append("<img src='"+res.src+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }
                    }else{
                        //判断......是否有，在进行块标签或隐藏
                        var num=document.getElementById('h1_id');
                        if(num!=null){
                            $("#img_div4").append("<img src='"+res.src+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }else{
                            $("#img_div4").append("<img src='"+res.src+"' class='uploadImg uploadImg2' style='display: block'/>");
                        }
                    }
                }else{
                    //添加预览图，加父边框，变成行内块标签
                    $("#img_div2").attr("style","display:block");
                    $("#img_div4").append("<img src='"+res.src+"' class='uploadImg'/>");
                    $("#img_div4").css({"border":"1px #D7D7D7 solid"});
                }

            }

        },
        drag : true,
        accept : 'file',
        multiple : true,
        data : {}
    });



    //上传视频
    upload.render({
        elem : '#uploadBtn2',
        url : '/SyteacherSelController/uploadAttachments',//上传接口

        done : function(res) {


            $("#div_video").append(" " +
                "<video id='my-video' class='video-js' controls preload='auto' data-setup='{}'> " +
                "<source src='"+res.src+"' class='uploadVideo' type='video/mp4'/> " +
                "</video>"
            );

            //视频截取
            var arrayImgs=res.src;
            var courseUrl=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);
            $("#courseUrl").val(courseUrl);

            var vid = document.getElementById("my-video");
            vid.onloadedmetadata = function() {
                var courseTime=Math.ceil(vid.duration)
                $("#courseTime").val(courseTime);
            };


            element.progress('progressBar',100 + '%');
            setTimeout("fun()","2000");  //2000毫秒后执行test()函数，只执行一次。

        },
        drag : true,
        accept : 'video',
        multiple : true,
        data : { }
        ,progress: function(value){
            $("#progressBar").attr("style","display:block");
            if(value<99){
                element.progress('progressBar',value + '%');
            }

        }
    });

});
function fun() {
    $("#progressBar").attr("style","display:none");
}
//返回
function go() {
    history.go(-1)
}

//选择分类
function addProductCategory() {
    layer.open({
        type:2,
        title:'选择分类',
        area:['700px', '600px'],
        offset:'auto',
        shade:0.5,
        maxmin:true,
        content:"CourseCategory.html",
        success:function(layero,index){
            $("#p_id").attr("style","display:block");
        }
    });
    window.layer_1=layer;
}



//添加课程
layui.use('form',function () {
    var form=layui.form;
    form.on('submit(formDemo)', function(data){

        $.ajax({
            url:"/operation/addCourse",
            type:"post",
            dataType:"json",
            traditional: true,//ajax传递数组必须添加属性
            data:formData,
            success:function(data){
                if(data.code==0){
                    layer.msg('添加成功');
                }else{
                    layer.msg('添加失败');
                }
            }
        });
        return false;
    });

});

//上传课程视频，进行判断
function uploadBtn(){
    //判断是否已有视频，在进行上传
    var num=$("#my-video").length;
    if(num>=1){
        layer.msg("您已添加过课程", {icon: 5});
    }else{
        $('#uploadBtn2').click();
    }
}

//预览图点击展开
function uploadImg() {
    $("#up").attr("style","display:block");
    //预览图点击计数
    var num=0;
    num++;
    $("#num").val(num);
    $(".uploadImg2").attr("style","display:block");
    $("h1").remove(".h");
}

//收起预览图
function upUploadImg() {
    $(".uploadImg2").attr("style","display:none");
    $("#up").attr("style","display:none");
    $("#img_div4").append("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
}



//添加课程
layui.use('form',function () {
    var form=layui.form;
    form.on('submit(formDemo)', function(data){

        //判断是否添加过视频和图片
        var video=$("#my-video").length;
        var url=$("#url").val();

        if(url==""||url==null||url==undefined){
            layer.msg("请先添加课程图片", {icon: 5});
        }else if(video<1){
            layer.msg("请先添加课程视频", {icon: 5});
        }else{
            //处理预选图
            var str=[];
            $("#img_div4 img").each(function (a) {

                //获取图片后面路径 截取拼接
                var arrayImgs= ($(this).attr("src"));
                var img=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);

                if(subImg!=arrayImgs){
                    //获取当前登陆人员的信息
                    var create=localStorage.getItem("user");
                    create=JSON.parse(create);//将获取json字符串转json对象
                    var courseName=$("#courseName").val();
                    json={"pictureType":2,"url":img,"pictureName":courseName,"createUser":create.user_id};

                    str.push(json);
                }


            });

            //获取预览图
            var formData = data.field;
            formData.str=JSON.stringify(str);

            $.ajax({
                url:"/SyOperationController/addCourse",
                type:"post",
                dataType:"json",
                traditional: true,//ajax传递数组必须添加属性
                data:formData,
                success:function(data){
                    if(data.code==0){
                        layer.msg('添加成功');
                        history.go(-1);
                    }else{
                        layer.msg('添加失败');
                    }
                }
            });
        }
        return false;
    });

});


